package com.fadadianqi.logistics.search;

import com.fadadianqi.logistics.customer.model.Village;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.search.AbstractIndexService;
import com.fadadianqi.search.IndexPathHelper;
import com.fadadianqi.search.IndexPathHelper.IndexEnum;
import com.yeqiangwei.commons.util.StringHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VillageIndexService extends AbstractIndexService {

    private static final Log log = LogFactory.getLog(VillageIndexService.class);

    private IndexPathHelper indexPathHelper = new IndexPathHelper(IndexEnum.VILLAGE);

    private String newPath;

    public VillageIndexService() {

    }

    @Override
    protected String getCurrentIndexPath() {
        return indexPathHelper.getIndexPath();
    }


    @Override
    protected String getNewIndexPath() {
        newPath = indexPathHelper.createNewPath();
        return newPath;
    }

    @Override
    protected void changeIndexPath() {
        indexPathHelper.savePath(newPath);
    }


    protected void build(IndexWriter writer, boolean rebuild) throws Exception {
        List<Village> list = customerService.findAllVillage(null);
        for (Village village : list) {
            writer.addDocument(documentBuilder(village));
        }
        writer.optimize();
        writer.close(true);
    }

    protected Document documentBuilder(Object o) throws Exception {
        Village village = (Village) o;

        Document doc = new Document();

        Field id = new Field(Village.PROP_ID, village.getId(), Store.YES, Index.NOT_ANALYZED);
        doc.add(id);

        String str_name = village.getName();

        Field name = new Field(Village.PROP_NAME, str_name, Store.YES, Index.ANALYZED);
        doc.add(name);

        String str_pinyin = StringHelper.toPinyin(str_name, str_name);
        str_pinyin += super.toPinyinIndexContent(str_name);

        Field pinyin = new Field(Village.PROP_PINYIN, str_pinyin, Store.YES, Index.ANALYZED);
        doc.add(pinyin);

        String name_initial = StringHelper.getInitial(str_name, null);
        Field initial = new Field(Village.PROP_INITIAL, name_initial, Store.YES, Index.ANALYZED);
        doc.add(initial);

        log.debug("document: " + doc.toString());
        return doc;
    }

    private static Boolean IS_RUNNING = Boolean.FALSE;

    public synchronized boolean isRunning() {
        return IS_RUNNING;
    }

    protected synchronized void lock() {
        IS_RUNNING = true;
    }

    protected synchronized void unlock() {
        IS_RUNNING = false;
    }

    @Autowired
    private CustomerService customerService;
}
